/**
 * 绝对地址删除接口测试用例
 *
 * Created by raquelmao on 2020/07/10.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));
const common = require('./../../../common');
const Administrator = require('./../../../../models/administrator');
const uri = require('./../../uri');
const benchmark = require('./../../../../benchmark/analyse-files/self_designed');

module.exports = describe('/DELETE functions/:functionId/fixed-addresses/:fixedAddressName.json', function () {
  const administrator = new Administrator();
  const newUserIds = [];
  let projectVersion = {};
  let func = {};
  function url(fixedAddressName, functionId) {
    if (!functionId) {
      functionId = func.functionId;
    }
    return `/functions/${functionId}/fixed-addresses/${fixedAddressName}.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((project) => project.createRandomVersion())
      .then((res) => {
        projectVersion = res;
        return projectVersion.getAllFunctions(benchmark);
      })
      .then(([item]) => {
        func = item;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'delete');
  common.should.be.tester(uri, url, 'delete');

  // 正常删除
  const variable = { fixedAddressName: `0x${Math.floor(Math.random() * 100000000).toString(16)}`, length: '2', pointerTargets: '{"偏移量 1":{"@type":"", "@length":"19", "@elementType": "int" }}' };
  it('it should DELETE fixed address', function (done) {
    const { fixedAddressName, length, pointerTargets } = variable;
    func.addFixedAddressVariable(fixedAddressName, length, pointerTargets)
      .then(() => {
        // 删除
        chai.request(uri)
          .delete(url(variable.fixedAddressName, func.functionId))
          .set('Authorization', `Bearer ${projectVersion.project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(204);
            done();
          });
      })
      .catch(done);
  });

  // // 删除不存在的指针目标
  // it('it should fail to delete fixed address when the variable is not exists', function (done) {
  //   new Promise((resolve) => {
  //     // 删除
  //     chai.request(uri)
  //       .delete(url(variable.fixedAddressName, func.functionId))
  //       .set('Authorization', `Bearer ${projectVersion.project.creator.token.accessToken}`)
  //       .end((err, res) => {
  //         res.should.have.status(404);
  //         common.should.be.errorMessage(res.body);
  //         resolve();
  //       });
  //   })
  //     .then(done)
  //     .catch(done);
  // });
});
